﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：GameStateManager
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/28 10:59:40
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System;
using TouchFish.StateMachine;

namespace TouchFish.GameState
{
    public abstract class GameStateManager : IGameStateManager
    {
        private IStateMachine<IGameStateManager> _gameStateMachine;
        private IStateMachineManager _stateMachineManager;

        public GameStateBase CurrentGameState
        {
            get
            {
                return (GameStateBase)_gameStateMachine.CurrentState;
            }
        }

        public void Initialize(StateMachineManager stateMachineManager, params GameStateBase[] procedures)
        {
            if (stateMachineManager == null)
            {
                throw new Exception("_stateMachineManager is invalid.");
            }

            _stateMachineManager = stateMachineManager;
            _gameStateMachine = _stateMachineManager.CreateFsm(this, procedures);
        }

        public GameStateBase GetGameState<T>() where T : GameStateBase
        {
            if (_gameStateMachine == null)
            {
                throw new Exception("You must initialize GameStateManager first.");
            }

            return _gameStateMachine.GetState<T>();
        }

        public GameStateBase GetGameState(Type gameStateType)
        {
            if (_gameStateMachine == null)
            {
                throw new Exception("You must initialize GameStateManager first.");
            }

            return (GameStateBase)_gameStateMachine.GetState(gameStateType);
        }

        public bool HasGameState<T>() where T : GameStateBase
        {
            if (_gameStateMachine == null)
            {
                throw new Exception("You must initialize GameStateManager first.");
            }

            return _gameStateMachine.HasState<T>();
        }

        public bool HasGameState(Type gameStateType)
        {
            if (_gameStateMachine == null)
            {
                throw new Exception("You must initialize GameStateManager first.");
            }

            return _gameStateMachine.HasState(gameStateType);
        }

        public void StartState(Type gameStateType)
        {
            if (_gameStateMachine == null)
            {
                throw new Exception("You must initialize GameStateManager first.");
            }

            _gameStateMachine.Start(gameStateType);
        }

        public void StartState<T>() where T : GameStateBase
        {
            if (_gameStateMachine == null)
            {
                throw new Exception("You must initialize GameStateManager first.");
            }

            _gameStateMachine.Start<T>();
        }
    }
}